package controle.service;

import java.sql.*;

public class ConnectionBDService 
{
						private Connection conn = null;
					    private Statement stmt = null; //statement每次执行sql语句，相关数据库都要执行sql语句的编译.
					    private ResultSet rs = null;

					    private PreparedStatement pstmt = null;//preparedstatement是预编译得,preparedstatement支持批处理.


					    
					    public Connection getConn()
					    {

					        try {

					            Class.forName("com.mysql.jdbc.Driver");

					            conn=DriverManager.getConnection("jdbc:mysql://localhost/gjo2m", "root", "");

					        	} catch (ClassNotFoundException e) 
					        	{

					        		e.printStackTrace();

					        	} catch (SQLException e) {

					        		e.printStackTrace();

					        	}
					        	return conn;
					    }
					    

					    public ResultSet getResultSet(String sql)
					    {

					        try {

					            if(conn==null||conn.isClosed()) conn=this.getConn();

					            stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

					            rs=stmt.executeQuery(sql);

					        } catch (SQLException e) {

					          e.printStackTrace();

					        }

					        return rs;
					        
					    
					    }

					    



					    public int excuteUpdate(String sql)
					    {

					        int result=0;

					        try {

					            if(conn==null||conn.isClosed()) conn=this.getConn();

					            result=conn.createStatement().executeUpdate(sql);

					        } catch (SQLException e) 
					        {


					            e.printStackTrace();

					        }

					        

					        return result;
					        
					        
					    }
					    
					    public PreparedStatement getPreparedStatement(String sql){

					        try {

					            if(conn==null||conn.isClosed()) conn=this.getConn();

					            pstmt=conn.prepareStatement(sql);

					        } catch (SQLException e) {

					          e.printStackTrace();

					        }

					        return pstmt;

					    }
					    
					    public void closeConn(){

					        try{

					            if(rs!=null){

					                rs.close();

					                rs=null;

					            }

					            if(stmt!=null){

					                stmt.close();

					                stmt=null;

					            }

					            if(conn!=null){

					                conn.close();

					                conn=null;

					            }

					        }catch (SQLException e) {

					       System.out.println("database est fermé");

					        }
					    }	
					        


					        




			public int verifierLogin(String nomClient,String pwdClient,int id_Client){

				int result = -1;

				String sql = "select * from client where idClient="+id_Client+" and nomClient='"+nomClient+"' and pwdClient='"+pwdClient+"'  ;";

				ResultSet rs = ((ConnectionBDService) conn).getResultSet(sql);

				try {

					if(rs.next()){

						if(id_Client == 0){

							result = 0;

						}

						else{

							result = 1;

						}

					}

					else{

						result = -1;

					}

				} catch (SQLException e) {

					result = -1;

					e.printStackTrace();

				}

				((ConnectionBDService) conn).closeConn();

				return result;

			}



		}


